from marshmallow import Schema, fields, validate, validates, ValidationError
import re
from datetime import datetime

class ContactInfoSchema(Schema):
    """联系信息模式"""
    phone = fields.Str()
    email = fields.Email()
    office = fields.Str()

    def validate_phone(self, value, **kwargs):
        """验证手机号格式"""
        if value and not re.match(r'^\d{11}$', value):
            raise ValidationError('手机号格式不正确')
        return value

class DoctorSchema(Schema):
    """医生模式"""
    userId = fields.Str()
    staffId = fields.Str(required=True)
    name = fields.Str(required=True)
    gender = fields.Str(validate=validate.OneOf(['男', '女', '其他']))
    title = fields.Str()
    departmentId = fields.Str(required=True)
    specialties = fields.List(fields.Str())
    contactInfo = fields.Nested(ContactInfoSchema)
    biography = fields.Str()
    education = fields.List(fields.Str())
    certifications = fields.List(fields.Str())
    photo = fields.Str()
    status = fields.Str(validate=validate.OneOf(['在职', '离职', '停诊']), dump_default='在职')
    createdAt = fields.DateTime(dump_default=datetime.utcnow)
    updatedAt = fields.DateTime(dump_default=datetime.utcnow)

class DoctorUpdateSchema(Schema):
    """医生更新模式"""
    name = fields.Str()
    gender = fields.Str(validate=validate.OneOf(['男', '女', '其他']))
    title = fields.Str()
    departmentId = fields.Str()
    specialties = fields.List(fields.Str())
    contactInfo = fields.Nested(ContactInfoSchema)
    biography = fields.Str()
    education = fields.List(fields.Str())
    certifications = fields.List(fields.Str())
    photo = fields.Str()
    status = fields.Str(validate=validate.OneOf(['在职', '离职', '停诊']))

class DoctorQuerySchema(Schema):
    """医生查询模式"""
    departmentId = fields.Str()
    name = fields.Str()
    title = fields.Str()
    status = fields.Str(validate=validate.OneOf(['在职', '离职', '停诊', 'all']))
    specialty = fields.Str() 